//
//  XCCellForScroller.m
//  TTLestening
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCCellForScroller.h"
#import "XCModelForFind.h"
#import <SDWebImage/UIImageView+WebCache.h>
#define WIDGHT  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds..size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667



@interface XCCellForScroller ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scroller;
@property (weak, nonatomic) IBOutlet UIPageControl *pageContro;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UIImageView *imageForScroll;

@end


@implementation XCCellForScroller

- (void)awakeFromNib {
    // Initialization code
   
   
    self.scroller.delegate = self;
    self.scroller.pagingEnabled = YES;
    self.scroller.bounces = NO;
    
    
}

- (void)passModel:(NSMutableArray *)array {

    
    self.pageContro.numberOfPages = array.count;

    self.array = [NSMutableArray array];
    
    if (array.count == array.count) {
        
        [array addObject:[array firstObject]];
        [array insertObject:[array objectAtIndex:array.count-2] atIndex:0];
        
        
    }
    
    self.array = [NSMutableArray arrayWithArray:array];
    self.scroller.contentSize = CGSizeMake(array.count * [UIScreen mainScreen].bounds.size.width, 0);

    self.scroller.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    
    
    for (int i = 0; i < self.array.count; i++) {
        
        // 每页轮播图的 image
        self.imageForScroll = [[UIImageView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, self.contentView.frame.size.height)];
        
       XCModelForFind *model = self.array[i];
        
        // 给每个 image 赋值
        [self.imageForScroll sd_setImageWithURL:[NSURL URLWithString:model.picUrl]];
        [self.scroller addSubview:self.imageForScroll];

        
    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.x == 0) {
        
        self.scroller.contentOffset = CGPointMake((self.array.count - 2) * WIDGHT, 0);
        
    } else if (self.scroller.contentOffset.x == (self.array.count - 1) * WIDGHT) {
    
        self.scroller.contentOffset = CGPointMake(WIDGHT, 0);
    
    
    }
    


}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
